<?php

namespace App\Kuafu\Order\Service;

use App\Kuafu\Base\Service\BaseService;
use CodeItNow\BarcodeBundle\Utils\QrCode;

class OrderCodeService extends BaseService
{

    /**
     * 生成小程序二维码
     *
     * @param int $shopId 门店编号
     * @param int $dId 渠道 Id
     * @param bool $needCode 是否需要二维码
     * @param int $sourceType 1-分销渠道
     * @return string[]
     * @throws \Exception
     */
    public static function genDistributionOrderCode(
        int $shopId,
        int $dId,
        bool $needCode = false,
        int $sourceType = 1
    ): array
    {
        $url = config('other.QR_URL') . '/distribution/order?';

        $params = [
            'shopId'         => $shopId,
            'distributionId' => $dId,
            'sourceType'     => $sourceType,
        ];

        $fullUrl = $url . http_build_query($params);

        $result = [
            'fullUrl' => $fullUrl
        ];

        if ($needCode) {
            $qrCode             = (new QrCode())->setText($fullUrl)->setImageType(QrCode::IMAGE_TYPE_PNG);
            $qrCode             = 'data:' . $qrCode->getContentType() . ';base64,' . $qrCode->generate();
            $result['imageUrl'] = $qrCode;
        }

        return $result;
    }

}
